package com.beinhospital_sw21_h2_05.beinhospitalsw21h205.service.impl;

import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.entity.Clerk;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.entity.Linshi3;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.entity.UserTable;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.mapper.ClerkMapper;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.service.ClerkService;
import com.beinhospital_sw21_h2_05.beinhospitalsw21h205.utils.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClerkService的实现类
 */
@Service
public class ClerkServiceImpl implements ClerkService {
    @Autowired
    private ClerkMapper clerkMapper;

    /**
     * 分页查询Clerk信息
     *
     * @param page      当前页码
     * @param pageSize  每页条数
     * @param clerkName 查询条件，职员名
     * @return 分页结果
     */
    @Override
    public PageBean page(Integer page, Integer pageSize, String clerkName) {
        PageHelper.startPage(page, pageSize);
        List<Linshi3> clerkList = clerkMapper.list(clerkName);
        Page<Linshi3> p = (Page<Linshi3>) clerkList;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    /**
     * 根据clerkID删除Clerk信息
     *
     * @param clerkID 职员ID
     */
    @Override
    public void delete(Integer clerkID) {
        Integer userID = clerkMapper.getUserID2(clerkID);
        clerkMapper.delete(clerkID);
        clerkMapper.deleteUser(userID);
    }

    /**
     * 新增Clerk信息
     *
     * @param linshi3 包含Clerk信息的临时对象
     */
    @Override
    public void add(Linshi3 linshi3) {
        Clerk clerk = new Clerk();
        UserTable user = new UserTable();
        user.setRole(2);
        user.setUsername(linshi3.getUsername());
        user.setPassword(linshi3.getPassword());
        clerkMapper.insertUser(user);
        clerk.setUserID(clerkMapper.getUserID(linshi3.getUsername()));
        clerk.setClerkName(linshi3.getClerkName());
        clerkMapper.insert(clerk);
    }

    /**
     * 更新Clerk信息
     *
     * @param linshi3 包含Clerk信息的临时对象
     */
    @Override
    public void update(Linshi3 linshi3) {
        Clerk clerk = new Clerk();
        UserTable user = new UserTable();
        user.setUsername(linshi3.getUsername());
        user.setPassword(linshi3.getPassword());
        user.setUserID(clerkMapper.getUserID2(linshi3.getClerkID()));
        clerkMapper.updateUser(user);
        clerk.setClerkID(linshi3.getClerkID());
        clerk.setClerkName(linshi3.getClerkName());
        clerkMapper.update(clerk);
    }
}
